package com.example.farm.controller;


import com.example.farm.Bean.Order;
import com.example.farm.Bean.Product;
import com.example.farm.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 姚
 * @since 2023-04-15
 */
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;


    private ArrayList<Product> order = new ArrayList<>();

    private  double sum;

    //更新商品信息
    @RequestMapping("/update")
    public int updateProduct(@RequestBody(required = false) Product product){
        return productService.updateProduct(product);
    }

    //查询所有商品信息
    @RequestMapping(value = "/selectAll",produces = "application/json;charset=UTF-8")
    public Object selectAll(){
        return productService.selectAll();
    }

    //向订单里面添加商品
    @RequestMapping(value = "/addProduct" ,produces = "application/json;charset=UTF-8")
    public void addProduct(@RequestBody(required = false)Product product){
        System.out.println(product);
        this.order.add(product);

    }

    //计算商品总价
    @RequestMapping(value = "/getSum",produces = "application/json;charset=UTF-8")
    public double calculate(){
        for (int i = 0; i < order.size(); i++) {
            this.sum = this.sum + order.get(i).getPrice() *order.get(i).getQuantity();
        }
        this.order.clear();
        return this.sum;
    }
    //获取商品数量
    @RequestMapping(value = "/getQuantity",produces = "application/json;charset=UTF-8")
    public int getQuantity(){
        return this.order.size();
    }

    //获取商品信息订单里面的商品信息
    @RequestMapping(value = "/getProduct",produces = "application/json;charset=UTF-8")
    public ArrayList<Product> getProduct(){
        return this.order;
    }




}

